package controller

import (
	"sharePicture/models/dao"
	"sharePicture/models/dto"
	"sharePicture/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 用户注册
func RegUser(r *gin.Context) {
	var re = dto.MessageDto{}
	u := dao.User{}
	r.Bind(&u)
	if u.Email == "" {
		r.JSON(200, re.Error("邮箱不能为空"))
		return
	}
	if u.Password == "" {
		r.JSON(200, re.Error("密码不能为空"))
		return
	}
	b := service.RegUser(&u)
	if b {
		r.JSON(200, re.Sucessful2("注册成功"))
		return
	} else {
		r.JSON(200, re.Error("注册失败，或账号已存在"))
		return
	}

}

// 用户登录
func LoginUser(r *gin.Context) {
	md := dto.MessageDto{}
	email := r.PostForm("email")
	password := r.PostForm("password")
	if email == "" || password == "" {
		r.JSON(200, md.Error("用户名或密码不能为空"))
		return
	}
	user, flag := service.LoginUser(email, password)
	if flag {
		r.JSON(200, md.Sucessful("成功", user))
		return
	} else {
		r.JSON(200, md.Error("用户名或密码错误"))
		return
	}

}

// 获取个人收藏
func GetOwnCollect(r *gin.Context) {
	uid, _ := strconv.Atoi(r.Query("uid"))
	page, _ := strconv.Atoi(r.Query("page"))
	pageSize, _ := strconv.Atoi(r.Query("pageSize"))
	pi, ok := service.GetOwnCollect(uid, page, pageSize)
	if ok {
		r.JSON(200, dto.MessageDto{}.Sucessful("成功", pi))
		return
	} else {
		r.JSON(200, dto.MessageDto{}.Error("失败"))
		return
	}

}

// 获取个人上传
func GetOwnUpload(r *gin.Context) {
	uid, _ := strconv.Atoi(r.Query("uid"))
	page, _ := strconv.Atoi(r.Query("page"))
	pageSize, _ := strconv.Atoi(r.Query("pageSize"))
	pi, ok := service.GetOwnUpload(uid, page, pageSize)
	if ok {
		r.JSON(200, dto.MessageDto{}.Sucessful("成功", pi))
		return
	} else {
		r.JSON(200, dto.MessageDto{}.Error("失败"))
		return
	}
}
